/**
 * Created by Administrator on 2016/10/27.
 */
window.onload = function () {

        // 添加上拉加载，下拉刷新
        var
            oScrollContainer = null,
            oScrollList = null,
            oScroll   = null,
            oPullDown = null,
            oPullUp   = null;

        setTimeout(function () {

            oScrollContainer = $('#container'),
                oScrollList = $('#list'),
                oPullDown = $('#pull-down');
            oPullUp = $('#pull-up');

            oScroll = new iScroll('container', {
                hScrollbar: false,
                vScrollbar: false,
                topOffset: oPullDown.height(),
                onScrollMove: function (){
                    if(this.y > 5 && !oPullDown.hasClass('active')) {
                        oPullDown.addClass('active').html('松开手刷新页面！');
                        this.minScrollY = 0;
                    } else if(this.y < 5 && oPullDown.hasClass('active')) {
                        oPullDown.removeClass('active').html('下拉刷新！');
                        this.minScrollY = - oPullDown.height();
                    } else if(this.y < this.maxScrollY && !oPullUp.hasClass('active')) {
                        oPullUp.addClass('active').html('松开手加载数据！');
                    } else if(this.y >=this.maxScrollY && oPullUp.hasClass('active')) {
                        oPullUp.removeClass('active').html('上拉加载！');
                    }
                },
                onScrollEnd:function () {
                    if(oPullDown.hasClass('active')) {
                        oPullDown.html('Loading....');
                        pullDownData();
                    } else if(oPullUp.hasClass('active')) {
                        oPullUp.html('Loading....');
                        pullUpData();
                    }
                },
                onRefresh: function (){
                    if(oPullDown.hasClass('active')) {
                        oPullDown.removeClass('active').html('下拉刷新！');
                    } else if(oPullUp.hasClass('active')) {
                        oPullUp.removeClass('active').html('上拉加载！');
                    }
                },
            });

            setTimeout(function () {
                oScrollContainer.css({left: 0});
            }, 300);
        }, 100);

        // 下拉刷新新的数据
        function pullDownData() {
            $.ajax({
                url: 'http://localhost/zouxiu.php',
                dataType: 'json',
                success: function (data) {
                    data.forEach(function (v, k) {
                        oScrollList.prepend('<li> ' +
                            '<div class="content"> ' +
                            '<div class="good-pic"> ' +
                            '<a href="javascript:;"> ' +
                            '<img src="' + v.url + '" alt=""> ' +
                            '</a> ' +
                            '</div> ' +
                            '<div class="good-inof"> ' +
                            '<div class="title">' +
                            '' + v.title + ' </div> ' +
                            '<div class="price"> ' +
                            '<div class="count"> ' +
                            '<p> ' +
                            '<span>￥0</span> ' +
                            '<span>￥288880</span> ' +
                            '</p> ' +
                            '<p>0折</p> ' +
                            '</div> ' +
                            '<div class="car"> ' +
                            '<a href="javascript:;" class="cart iconfont" data-id="'+ v.id +'" data-price="'+ v.price+'" data-title="'+ v.title+'" data-src="'+ v.url+'">&#xe653;</a> ' +
                            '</div> ' +
                            '</div> ' +
                            '</div> ' +
                            '</div> ' +
                            '</li>');
                    });
                    oScroll.refresh();
                }
            });
        }

        // 下拉刷新新的数据
      function pullUpData() {
            $.ajax({
                url: 'http://localhost/zouxiu.php',
                dataType: 'json',
                success: function (data) {
                    data.forEach(function (v, k) {
                        oScrollList.append('<li> ' +
                            '<div class="content"> ' +
                            '<div class="good-pic"> ' +
                            '<a href="javascript:;"> ' +
                            '<img src="' + v.url + '" alt=""> ' +
                            '</a> ' +
                            '</div> ' +
                            '<div class="good-inof"> ' +
                            '<div class="title">' +
                            '' + v.title + ' </div> ' +
                            '<div class="price"> ' +
                            '<div class="count"> ' +
                            '<p> ' +
                            '<span>￥0</span> ' +
                            '<span>￥288880</span> ' +
                            '</p> ' +
                            '<p>0折</p> ' +
                            '</div> ' +
                            '<div class="car"> ' +
                            '<a href="javascript:;" class="cart iconfont" data-id="'+ v.id +'" data-price="'+ v.price+'" data-title="'+ v.title+'" data-src="'+ v.url+'">&#xe653;</a> ' +
                            '</div> ' +
                            '</div> ' +
                            '</div> ' +
                            '</div> ' +
                            '</li>');
                    });
                    oScroll.refresh();
                }
            });
        }


   $("#list").click(function () {
       $("#list")[0].addEventListener("click",function(ev){//事件监听
            var e=ev||window.event;//事件
            var oTarget=e.srcElement||e.target;//点击的对象
            if(oTarget.nodeName=='A') {///判断点击的是不是div
                //var index=index($(this))

                // 要存储的商品信息：商品ID、商品名称、商品价格、商品数量、商品图片地址
                var
                    iGoodsId = $(e.target).attr('data-id'),
                    sGoodsTitle = $(e.target).attr('data-title'),
                    iGoodsPrice = $(e.target).attr('data-price'),
                    sGoodsSrc = $(e.target).attr('data-src');
                //console.log(sGoodsSrc)

                var
                    sGoods = getCookie('ggoods'),
                    bBtn = true;
                //判断是否有商品
                if (sGoods === undefined) {
                    var aGoods = [];
                } else {
                    var aGoods = JSON.parse(sGoods);
                }

                for (var i = 0; i < aGoods.length; i++) {
                    if (aGoods[i].goodsId == iGoodsId) {
                        aGoods[i].goodsNum++;
                        //console.log( aGoods[i].goodsNum)
                        bBtn = false;
                    }
                }

                if (bBtn) {
                    var oGoods = {
                        goodsId: iGoodsId,
                        goodsTitle: sGoodsTitle,
                        goodsPrice: iGoodsPrice,
                        goodsSrc: sGoodsSrc,
                        goodsNum: 1,
                    };
                    aGoods.push(oGoods);
                }
                //var aGoods = []; // 存储所有商品信息的数组

                console.log(aGoods);
                // 写入cookie
                setCookie('ggoods', JSON.stringify(aGoods), 7, '/');
                //console.log(sGoods)
            }
      })
    },false)





      /*  $("#list").click(function(ev){
        var e=ev||window.event;//事件委托
         var oTarget=e.toElement||e.target;//点击的对象
            console.log(oTarget);
         if(oTarget.nodeName=='A') {///判断点击的是不是div
             var

                 iGoodsId = $(e.target).attr('data-id'),
                 sGoodsTitle = $(e.target).attr('data-title'),
                 iGoodsPrice = $(e.target).attr('data-price'),
                 sGoodsSrc = $(e.target).attr('data-src');


             var
                 sGoods = getCookie('ggoods'),
                 bBtn = true;

             //判断是否有商品
             if (sGoods === undefined) {
                 var aGoods = [];
             } else {
                 var aGoods = JSON.parse(sGoods);
             }

             for (var i = 0; i < aGoods.length; i++) {
                 if (aGoods[i].goodsId == iGoodsId) {
                     aGoods[i].goodsNum++;
                     //console.log( aGoods[i].goodsNum)
                     bBtn = false;
                 }
             }

             if (bBtn) {
                 var oGoods = {
                     goodsId: iGoodsId,
                     goodsTitle: sGoodsTitle,
                     goodsPrice: iGoodsPrice,
                     goodsSrc: sGoodsSrc,
                     goodsNum: 1,
                 };
                 aGoods.push(oGoods);
             }
             //var aGoods = []; // 存储所有商品信息的数组

             console.log(aGoods);
             // 写入cookie
             setCookie('ggoods', JSON.stringify(aGoods), 7, '/');
             //console.log(sGoods)
             /!*}*!/

         }
    },false)*/
};